<template>
  <a-select
    v-bind="$attrs"
    :allow-clear="allowClear"
    :value="props.modelValue"
    :placeholder="placeholder"
    :options="options"
    @change="change"
  >
  </a-select>
</template>

<script lang="ts" setup>
import api from '@/api'
import { ref, onMounted } from 'vue'
import emitter from '@/utils/bus'

let options = ref<any[]>([])

interface SelectProps {
  placeholder?: string
  modelValue: any
  allowClear?: boolean
}

interface SelectEmits {
  (e: 'update:modelValue', value: string): void
  (e: 'change', value: string): void
}

const props = withDefaults(defineProps<SelectProps>(), {
  placeholder: '请选择单位名称',
  allowClear: true,
})

const emit = defineEmits<SelectEmits>()

onMounted(() => {
  getOptions()
})

const getOptions = async (): Promise<void> => {
  const { data } = await api.facilityLink(0)
  const list = data.data
  list.forEach((item) => {
    options.value.push({ value: item.ssid, label: item.ssmc })
  })
}

function change(value: any): void {
  emit('update:modelValue', value)
  emit('change', value)
  emitter.emit('unit-change', { value, clear: true })
}
</script>

<style lang="less" scoped></style>
